package com.syni.mdd.yhd.common.all.listen.video;

import com.alibaba.fastjson.JSON;
import com.syni.mdd.yhd.common.all.constants.Constants;
import com.syni.mdd.yhd.common.all.entity.BmsUserOperationLog;
import com.syni.mdd.yhd.common.all.service.BmsUserOperationLogService;
import com.syni.mdd.yhd.common.all.service.BmsVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentSkipListMap;

@Component
public class VideoGecEventListener implements ApplicationListener<VideoEvent> {

    final static String MODEL = "videoGec";

    @Autowired
    BmsUserOperationLogService bmsUserOperationLogService;

    @Override
    public void onApplicationEvent(VideoEvent videoEvent) {
        String model = videoEvent.getSource().toString();
        if(!MODEL.equals(model)){
            return;
        }
        String userId = videoEvent.getUserIdentify();
        String addr = videoEvent.getAddr();
        String longitude = videoEvent.getLongitude();
        String latitude = videoEvent.getLatitude();
        if(!userId.startsWith(BmsVideoService.visitorPrfix)){
            BmsUserOperationLog bmsUserOperationLog = new BmsUserOperationLog();
            bmsUserOperationLog.setUserId(Integer.parseInt(userId));
            Map<String,Object> content = new HashMap<>();
            content.put("addr",addr);
            content.put("longitude",longitude);
            content.put("latitude",latitude);
            bmsUserOperationLog.setOperationContent(JSON.toJSONString(content));
            bmsUserOperationLog.setOperationTime(new Date());
            bmsUserOperationLog.setOperationType(Constants.USER_VISIT_GEO);
            bmsUserOperationLog.setFromOrigin(Constants.USER_CLIENT);
            bmsUserOperationLogService.getBaseDao().save(bmsUserOperationLog);
        }
    }
}
